﻿/*
 * Copyright © 2011 Tristan Reeves, treeves@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Objects.Factory;
using Sprinkles.Secondary;
using Sprinkles.Tests.TestClasses;

namespace Sprinkles.Tests.DeploymentHosts
{
    public class DummyHost_InstanceFactoryWithArgs : DeploymentHost
    {
        protected override bool Include
        {
            get { return UserContext == "factory-config-instance-1"; }
        }

        string NO_CONSTRUCTOR = "instance-factory.no-constructor";
        string CONSTRUCTOR_BEFORE = "instance-factory.constructor.before";
        string INSTANCE_FACTORY_NAME = Qualify("instance-factory");

        public static string Qualify(string name)
        {
            return "DummyHost_InstanceFactoryWithArgs." + name;
        }
        private void RegisterDeployments1()
        {
            var ctr = 1;

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + "." + NO_CONSTRUCTOR))
                .Singleton(true)
                .CreatedBy.Constructor()
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a) => x.CreateOne(a), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1." + NO_CONSTRUCTOR)
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + "." + CONSTRUCTOR_BEFORE))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a) => x.CreateOne(a), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1." + CONSTRUCTOR_BEFORE)
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.after"))
                .Singleton(true)
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a) => x.CreateOne(a), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.after")
                .CreatedBy.Constructor()
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.no-constructor.notype"))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a) => x.UntypedCreateOne(a), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.no-constructor.notype")
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.before.notype"))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a) => x.UntypedCreateOne(a), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.before.notype")
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.after.notype"))
                .Singleton(false)
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a) => x.UntypedCreateOne(a), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.after.notype")
                .CreatedBy.Constructor()
                .Done();
        }
        private void RegisterDeployments2()
        {
            var ctr = 2;

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + "." + NO_CONSTRUCTOR))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a, object b) => x.CreateOne(a, b), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1." + NO_CONSTRUCTOR)
                    .SetLiteral("2." + NO_CONSTRUCTOR)
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + "." + CONSTRUCTOR_BEFORE))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a, object b) => x.CreateOne(a, b), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1." + CONSTRUCTOR_BEFORE)
                    .SetLiteral("2." + CONSTRUCTOR_BEFORE)
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.after"))
                .Singleton(false)
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a, object b) => x.CreateOne(a, b), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.after")
                    .SetLiteral("2.instance-factory.constructor.after")
                .CreatedBy.Constructor()
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.no-constructor.notype"))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a, object b) => x.UntypedCreateOne(a, b), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.no-constructor.notype")
                    .SetLiteral("2.instance-factory.no-constructor.notype")
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.before.notype"))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a, object b) => x.UntypedCreateOne(a, b), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.before.notype")
                    .SetLiteral("2.instance-factory.constructor.before.notype")
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.after.notype"))
                .Singleton(false)
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a, object b) => x.UntypedCreateOne(a, b), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.after.notype")
                    .SetLiteral("2.instance-factory.constructor.after.notype")
                .CreatedBy.Constructor()
                .Done();
        }
        private void RegisterDeployments3()
        {
            var ctr = 3;

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + "." + NO_CONSTRUCTOR))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c) => x.CreateOne(a, b, c), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1." + NO_CONSTRUCTOR)
                    .SetLiteral("2." + NO_CONSTRUCTOR)
                    .SetLiteral("3." + NO_CONSTRUCTOR)
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + "." + CONSTRUCTOR_BEFORE))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c) => x.CreateOne(a, b, c), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1." + CONSTRUCTOR_BEFORE)
                    .SetLiteral("2." + CONSTRUCTOR_BEFORE)
                    .SetLiteral("3." + CONSTRUCTOR_BEFORE)
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.after"))
                .Singleton(false)
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c) => x.CreateOne(a, b, c), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.after")
                    .SetLiteral("2.instance-factory.constructor.after")
                    .SetLiteral("3.instance-factory.constructor.after")
                .CreatedBy.Constructor()
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.no-constructor.notype"))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c) => x.UntypedCreateOne(a, b, c), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.no-constructor.notype")
                    .SetLiteral("2.instance-factory.no-constructor.notype")
                    .SetLiteral("3.instance-factory.no-constructor.notype")
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.before.notype"))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c) => x.UntypedCreateOne(a, b, c), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.before.notype")
                    .SetLiteral("2.instance-factory.constructor.before.notype")
                    .SetLiteral("3.instance-factory.constructor.before.notype")
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.after.notype"))
                .Singleton(false)
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c) => x.UntypedCreateOne(a, b, c), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.after.notype")
                    .SetLiteral("2.instance-factory.constructor.after.notype")
                    .SetLiteral("3.instance-factory.constructor.after.notype")
                .CreatedBy.Constructor()
                .Done();
        }
        private void RegisterDeployments4()
        {
            var ctr = 4;

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + "." + NO_CONSTRUCTOR))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c, object d) => x.CreateOne(a, b, c, d), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1." + NO_CONSTRUCTOR)
                    .SetLiteral("2." + NO_CONSTRUCTOR)
                    .SetLiteral("3." + NO_CONSTRUCTOR)
                    .SetLiteral("4." + NO_CONSTRUCTOR)
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + "." + CONSTRUCTOR_BEFORE))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c, object d) => x.CreateOne(a, b, c, d), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1." + CONSTRUCTOR_BEFORE)
                    .SetLiteral("2." + CONSTRUCTOR_BEFORE)
                    .SetLiteral("3." + CONSTRUCTOR_BEFORE)
                    .SetLiteral("4." + CONSTRUCTOR_BEFORE)
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.after"))
                .Singleton(false)
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c, object d) => x.CreateOne(a, b, c, d), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.after")
                    .SetLiteral("2.instance-factory.constructor.after")
                    .SetLiteral("3.instance-factory.constructor.after")
                    .SetLiteral("4.instance-factory.constructor.after")
                .CreatedBy.Constructor()
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.no-constructor.notype"))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c, object d) => x.UntypedCreateOne(a, b, c, d), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.no-constructor.notype")
                    .SetLiteral("2.instance-factory.no-constructor.notype")
                    .SetLiteral("3.instance-factory.no-constructor.notype")
                    .SetLiteral("4.instance-factory.no-constructor.notype")
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.before.notype"))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c, object d) => x.UntypedCreateOne(a, b, c, d), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.before.notype")
                    .SetLiteral("2.instance-factory.constructor.before.notype")
                    .SetLiteral("3.instance-factory.constructor.before.notype")
                    .SetLiteral("4.instance-factory.constructor.before.notype")
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.after.notype"))
                .Singleton(false)
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c, object d) => x.UntypedCreateOne(a, b, c, d), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.after.notype")
                    .SetLiteral("2.instance-factory.constructor.after.notype")
                    .SetLiteral("3.instance-factory.constructor.after.notype")
                    .SetLiteral("4.instance-factory.constructor.after.notype")
                .CreatedBy.Constructor()
                .Done();
        }
        private void RegisterDeployments5()
        {
            var ctr = 5;

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + "." + NO_CONSTRUCTOR))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c, object d, object e) => x.CreateOne(a, b, c, d, e), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1." + NO_CONSTRUCTOR)
                    .SetLiteral("2." + NO_CONSTRUCTOR)
                    .SetLiteral("3." + NO_CONSTRUCTOR)
                    .SetLiteral("4." + NO_CONSTRUCTOR)
                    .SetLiteral("5." + NO_CONSTRUCTOR)
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + "." + CONSTRUCTOR_BEFORE))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c, object d, object e) => x.CreateOne(a, b, c, d, e), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1." + CONSTRUCTOR_BEFORE)
                    .SetLiteral("2." + CONSTRUCTOR_BEFORE)
                    .SetLiteral("3." + CONSTRUCTOR_BEFORE)
                    .SetLiteral("4." + CONSTRUCTOR_BEFORE)
                    .SetLiteral("5." + CONSTRUCTOR_BEFORE)
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.after"))
                .Singleton(false)
                .CreatedBy.InstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c, object d, object e) => x.CreateOne(a, b, c, d, e), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.after")
                    .SetLiteral("2.instance-factory.constructor.after")
                    .SetLiteral("3.instance-factory.constructor.after")
                    .SetLiteral("4.instance-factory.constructor.after")
                    .SetLiteral("5.instance-factory.constructor.after")
                .CreatedBy.Constructor()
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.no-constructor.notype"))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c, object d, object e) => x.UntypedCreateOne(a, b, c, d, e), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.no-constructor.notype")
                    .SetLiteral("2.instance-factory.no-constructor.notype")
                    .SetLiteral("3.instance-factory.no-constructor.notype")
                    .SetLiteral("4.instance-factory.no-constructor.notype")
                    .SetLiteral("5.instance-factory.no-constructor.notype")
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.before.notype"))
                .Singleton(false)
                .CreatedBy.Constructor()
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c, object d, object e) => x.UntypedCreateOne(a, b, c, d, e), INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.before.notype")
                    .SetLiteral("2.instance-factory.constructor.before.notype")
                    .SetLiteral("3.instance-factory.constructor.before.notype")
                    .SetLiteral("4.instance-factory.constructor.before.notype")
                    .SetLiteral("5.instance-factory.constructor.before.notype")
                .Done();

            Register<InstanceFactoryTestClass>()
                .Name(Qualify(ctr + ".instance-factory.constructor.after.notype"))
                .Singleton(false)
                .CreatedBy.UnsafeInstanceFactoryMethod((TestInstanceFactory x, object a, object b, object c, object d, object e) => x.UntypedCreateOne(a, b, c, d, e), () => INSTANCE_FACTORY_NAME)
                    .SetLiteral("1.instance-factory.constructor.after.notype")
                    .SetLiteral("2.instance-factory.constructor.after.notype")
                    .SetLiteral("3.instance-factory.constructor.after.notype")
                    .SetLiteral("4.instance-factory.constructor.after.notype")
                    .SetLiteral("5.instance-factory.constructor.after.notype")
                .CreatedBy.Constructor()
                .Done();
        }

        protected override void RegisterDeployments()
        {
            Register<TestInstanceFactory>()
                .Name(INSTANCE_FACTORY_NAME)
                .Singleton(true)
                .Done();

            RegisterDeployments1();
            RegisterDeployments2();
            RegisterDeployments3();
            RegisterDeployments4();
            RegisterDeployments5();
        }
    }
}
